package com.system.cacheCenterBackend.service.cacheService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Redis缓存服务实现类
 */
@Service
public class RedisCacheServiceImpl implements RedisCacheService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public boolean set(String key, String value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean set(String key, String value, long timeout) {
        try {
            redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public String get(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean delete(String key) {
        try {
            return redisTemplate.delete(key);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean exists(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            return false;
        }
    }

    // ==================== List 操作 ====================

    @Override
    public Long leftPush(String key, String value) {
        try {
            return redisTemplate.opsForList().leftPush(key, value);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public Long rightPush(String key, String value) {
        try {
            return redisTemplate.opsForList().rightPush(key, value);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public String leftPop(String key) {
        try {
            return redisTemplate.opsForList().leftPop(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String rightPop(String key) {
        try {
            return redisTemplate.opsForList().rightPop(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<String> listRange(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Long listSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            return 0L;
        }
    }

    // ==================== Set 操作 ====================

    @Override
    public Long setAdd(String key, String... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public Long setRemove(String key, String... values) {
        try {
            return redisTemplate.opsForSet().remove(key, (Object[]) values);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public Set<String> setMembers(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Boolean setIsMember(String key, String value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long setSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            return 0L;
        }
    }

    // ==================== Hash 操作 ====================

    @Override
    public void hashPut(String key, String field, String value) {
        try {
            redisTemplate.opsForHash().put(key, field, value);
        } catch (Exception e) {
            // 记录日志或处理异常
        }
    }

    @Override
    public String hashGet(String key, String field) {
        try {
            Object value = redisTemplate.opsForHash().get(key, field);
            return value != null ? value.toString() : null;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Long hashDelete(String key, String... fields) {
        try {
            return redisTemplate.opsForHash().delete(key, (Object[]) fields);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public Map<String, String> hashGetAll(String key) {
        try {
            Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
            Map<String, String> result = new java.util.HashMap<>();
            for (Map.Entry<Object, Object> entry : entries.entrySet()) {
                result.put(entry.getKey().toString(), entry.getValue().toString());
            }
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Boolean hashExists(String key, String field) {
        try {
            return redisTemplate.opsForHash().hasKey(key, field);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long hashSize(String key) {
        try {
            return redisTemplate.opsForHash().size(key);
        } catch (Exception e) {
            return 0L;
        }
    }

    // ==================== ZSet 操作 ====================

    @Override
    public Boolean zsetAdd(String key, String value, double score) {
        try {
            return redisTemplate.opsForZSet().add(key, value, score);
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long zsetRemove(String key, String... values) {
        try {
            return redisTemplate.opsForZSet().remove(key, (Object[]) values);
        } catch (Exception e) {
            return 0L;
        }
    }

    @Override
    public Set<String> zsetRange(String key, long start, long end) {
        try {
            return redisTemplate.opsForZSet().range(key, start, end);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Set<String> zsetRangeByScore(String key, double minScore, double maxScore) {
        try {
            return redisTemplate.opsForZSet().rangeByScore(key, minScore, maxScore);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Double zsetScore(String key, String value) {
        try {
            return redisTemplate.opsForZSet().score(key, value);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Long zsetSize(String key) {
        try {
            return redisTemplate.opsForZSet().size(key);
        } catch (Exception e) {
            return 0L;
        }
    }
    
    @Override
    public Map<String, String> getHash(String key) {
        return hashGetAll(key);
    }
    
    @Override
    public List<String> getList(String key) {
        try {
            return listRange(key, 0, -1);
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    public Set<String> getSet(String key) {
        return setMembers(key);
    }
    
    @Override
    public Set<String> getZSet(String key) {
        try {
            return zsetRange(key, 0, -1);
        } catch (Exception e) {
            return null;
        }
    }
}